package com.zhanggm.springcloud.config;

import com.netflix.loadbalancer.*;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RibbonConfig {

    // LoadBalanced 添加这个注解，在调用的时候就有负载均衡的功能
    //默认的负载均衡算法是轮询
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    //配置负载均衡算法
    //默认的负载均衡算法是轮询
    @Bean
    public IRule iRule(){
        //切换负载均衡算法
        return new RandomRule();
//        return new RoundRobinRule();
        /*return new PredicateBasedRule() {
            @Override
            public AbstractServerPredicate getPredicate() {
                return null;
            }
        };*/
    }

}
